package net.neptech.node;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author HuangZhengmin
 * @desc  最终推荐节点 - 生成完整的诊断建议
 * @Date 2025/8/19 17:44
 * @Version 1.0
 */
@Component
public class FinalRecommendationNode implements NodeAction {
  private static final Logger logger = LoggerFactory.getLogger(FinalRecommendationNode.class);

  @Autowired
  private ChatClient chatClient;

  @Override
  public Map<String, Object> apply(OverAllState state) {
    logger.info("进入最终推荐节点");
    Map<String, Object> result = new HashMap<>();

    // 收集所有相关信息
    String symptoms = (String) state.value("standardized_symptoms").orElse("");
    String diseases = (String) state.value("disease_inference").orElse("");
    String departments = (String) state.value("department_recommendation").orElse("");
    String riskLevel = (String) state.value("risk_level").orElse("low_risk");

    // 生成最终建议
    String prompt = String.format(
            "患者症状: %s\n" +
                    "可能的疾病: %s\n" +
                    "推荐科室: %s\n" +
                    "风险等级: %s\n" +
                    "请生成最终的诊断建议，包括:\n" +
                    "1. 简要总结分析\n" +
                    "2. 就诊建议和注意事项\n" +
                    "3. 重要声明（系统建议不能替代医生诊断）",
            symptoms, diseases, departments, riskLevel);

    String recommendation = chatClient.prompt()
            .user(prompt)
            .call()
            .content();

    // 更新状态
    result.put("final_recommendation", recommendation);

    logger.info("最终推荐已生成");
    return result;
  }
}
